﻿using System;
using System.Web;
using System.Web.Security;
using visinia.SmartEngine;
using viWiki.Classes;
using viWiki.Controllers;

public partial class Admin_Pages_viWiki_WikiPageEditor : System.Web.UI.UserControl
{    
    protected void Page_Load(object sender, EventArgs e)
    {
        WebPageController.AddScriptLink(Page, WebRoots.GetAdminPagesRoot + "viWiki/Pages/Wiki.js");
        WebPageController.AddCssLink(Page, WebRoots.GetAdminPagesRoot + "viWiki/Pages/Wiki.css");

        if (!IsPostBack)
        {
            AUTHORS.DataSource = Membership.GetAllUsers();
            AUTHORS.DataBind();            
            //create a new wiki page
            if (Request.QueryString["WikiPageId"] == null)
            {
                DATE.Text = DateTime.Now.ToString();
            }
            else
            {// edit an already existing wiki page
                int WikiPageId = Convert.ToInt32(Request.QueryString["WikiPageId"]);                
                WikiPage wikipage = WikiPageController.GetWikiPage(WikiPageId);
                NAME.Text = wikipage.Name;
                DATE.Text = wikipage.Date.ToString();
                CONTENT.Text = wikipage.Content;
                NAMESPACE.SelectedValue = wikipage.NamespaceInfoId.ToString();
                if (Request.QueryString["saved"] == "true")
                {
                    MESSAGE.Text = "Wiki Page Saved  ";
                    PreviewLink.NavigateUrl = wikipage.Url;
                    MessageLocal.Visible = true;
                }
            }
        }
    }
    protected void SAVE_Click(object sender, EventArgs e)
    {
        //save a new wiki page
        if (Request.QueryString["WikiPageId"] == null)
        {
            WikiPage wikipage = new WikiPage();
            wikipage.Name = NAME.Text;
            wikipage.Author = AUTHORS.SelectedValue;
            wikipage.Date = Convert.ToDateTime(DATE.Text);
            wikipage.Content = CONTENT.Text;
            wikipage.Url = RemoveIllegalCharacters(wikipage.Name);
            wikipage.NamespaceInfoId = Convert.ToInt32(NAMESPACE.SelectedValue);
            int wikipageid = WikiPageController.AddWikiPage(wikipage);
            // reload the newly created wiki page
            Response.Redirect(SystemMenu.CallPage("viWiki", "EditPage", "WikiPageId", wikipageid.ToString()) + "&saved=true");
        }
        else
        {// update an already existing wiki page
            int wikipageid = 0;
            if (!Int32.TryParse(Request.QueryString["WikiPageId"], out wikipageid))
                return;
            WikiPage wikipage = WikiPageController.GetWikiPage(wikipageid);
            if (wikipage == null)
                return;
            wikipage.Name = NAME.Text;
            wikipage.Author = AUTHORS.SelectedValue;
            wikipage.Date = Convert.ToDateTime(DATE.Text);            
            wikipage.Content = CONTENT.Text;
            wikipage.Url = RemoveIllegalCharacters(wikipage.Name);
            //this wiki page might be the Main Page of another namespace so inform that namespace
            NamespaceInfo nspace = NamespaceController.GetNamespace(wikipage.NamespaceInfoId);
            if (nspace.DefaultWikiPageId == wikipage.WikiPageId)
            {
                nspace.DefaultWikiPageId = 0;
                NamespaceController.UpdateNamespace(nspace);
            }
            wikipage.NamespaceInfoId = Convert.ToInt32(NAMESPACE.SelectedValue);
            WikiPageController.UpdateWikiPage(wikipage);
            Response.Redirect(SystemMenu.CallPage("viWiki", "EditPage", "WikiPageId", wikipageid.ToString()) + "&saved=true");
        }
       
    }

    protected void CANCEL_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["WikiPageId"] == null)        
            Response.Redirect(Request.RawUrl);
        Response.Redirect(SystemMenu.CallPage("viWiki", "EditPage", "WikiPageId", Request.QueryString["WikiPageId"]));
    }

    private static string RemoveIllegalCharacters(string Title)
    {
        if (string.IsNullOrEmpty(Title))
            return Title;

        Title = Title.Replace(":", string.Empty);
        Title = Title.Replace("/", string.Empty);
        Title = Title.Replace("?", string.Empty);
        Title = Title.Replace("#", string.Empty);
        Title = Title.Replace("[", string.Empty);
        Title = Title.Replace("]", string.Empty);
        Title = Title.Replace("@", string.Empty);
        Title = Title.Replace("*", string.Empty);
        Title = Title.Replace(".", string.Empty);
        Title = Title.Replace(",", string.Empty);
        Title = Title.Replace("\"", string.Empty);
        Title = Title.Replace("&", string.Empty);
        Title = Title.Replace("'", string.Empty);
        Title = Title.Replace(" ", "-");
        return HttpUtility.UrlEncode(Title).Replace("%", string.Empty);
    }
}
